package com.haoshuang.zhouzhoubang.ui.dialog

import android.content.Context

import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.model.bean.ReasonBean
import com.haoshuang.zhouzhoubang.ui.adapter.ReasonAdapter


import com.lxj.xpopup.core.BottomPopupView
import com.mufeng.libs.utils.toast


class BgxDialog(
    context: Context,
    list8: ArrayList<String>,
    private val onSubmit: (str: String, remark: String) -> Unit
) : BottomPopupView(context) {
    override fun getImplLayoutId(): Int = R.layout.dialog_bgx
    var list = list8

    override fun initPopupContent() {
        super.initPopupContent()
        var rl_root = findViewById<ImageView>(R.id.tvGb2)
        var rvList = findViewById<RecyclerView>(R.id.rvList)
        var tvContent = findViewById<TextView>(R.id.tvContent)
        var tvGb = findViewById<TextView>(R.id.tvGb)
        var list2 = arrayListOf<ReasonBean>()
        for (b in list) {
            list2.add(ReasonBean(b, 0))
        }

        var adapter = ReasonAdapter(list2)
        rvList.layoutManager = LinearLayoutManager(context)
        rvList.adapter = adapter
        adapter.setOnItemClickListener { adapter, view, position ->
            if (list2[position].isCheck == 0) {
                for (b in list2) {
                    b.isCheck = 0
                }
                list2[position].isCheck = 1
                if (list2[position].reason == "其他原因") {
                    tvContent.isEnabled = true
                } else {
                    tvContent.isEnabled = false
                }

            } else {
                //  tvContent.gone()
                tvContent.isEnabled = false
                list2[position].isCheck = 0
            }
            adapter.notifyDataSetChanged()
        }
        var tvCommit = findViewById<TextView>(R.id.tvCommit)
        rl_root.setOnClickListener {
            dismiss()
        }
        tvGb.setOnClickListener {
            dismiss()
        }
        tvCommit.setOnClickListener {
            var check = 0
            var str = ""
            for (b in list2) {
                if (b.isCheck == 1) {
                    check = 1
                    str = b.reason
                }
            }
            if (check == 0) {
                toast("请选择在提交")
                return@setOnClickListener
            }


            if (str == "其他原因") {
                var str2 = tvContent.text.toString()
                if (str2.isNullOrBlank()) {
                    toast("请输入原因")
                } else {
                    dismiss()
                    onSubmit(str, str2)
                }

            } else {
                dismiss()
                onSubmit(str, "")
            }

        }
    }


}